package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.Customerservices;
import cn.chia.pojo.Peisongren;
import cn.chia.pojo.TblUser;
import cn.chia.pojo.User;
import cn.chia.service.CSService;
import cn.chia.service.IUserService;
import cn.chia.service.ManagerService;
import cn.chia.service.SenderService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.Md5Util;
import cn.chia.utils.Onmanager;

/*
 * 于辉  后台管理员Action
 */
@Controller
public class ManagerAction {
	private ManagerService managerService;        //管理员Service
	private SenderService senderService;          //送货员Service
	private CSService cSService;                  //客服Service
	private IUserService userService;             //用户Service
	private TblUser tblUser;                      //管理员对象
	private Peisongren peisongren;                //配送员对象
	private Customerservices customerservice;     //客服对象
	private User user;                            //用户对象
	private int id;                               //id属性
	private String username;                      //管理员用户名
	private String password;                      //密码
	private String verifycode;                    //页面输入的验证码
	private List<?> senderList;                   //配送员集合
	private List<?> csList;                       //客服集合
	private List<?> userList;                     //用户集合
	
	private int pageNo = 1;
	private int pageSize = 10;
	private String column = "tname";
	private String keyword="";
	private int count;
	/*
	 * 于辉  管理员登陆验证
	 */
	public String login() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		//验证码验证
		String scode = (String) request.getSession().getAttribute("VerifyCode");
		if(!verifycode.equalsIgnoreCase(scode)){
			request.setAttribute("error2", "验证码错误!!");
			return "error";
		}
		
		tblUser = managerService.findByUserPass(username,Md5Util.addMd5(password));
		if(tblUser==null){
			request.setAttribute("error","用户名或密码错误!");
			return "error";
		}else if(tblUser.getTblRole().getRoleId()!=1){
			request.setAttribute("error","您不是管理员，权限不足!");
			return "error";
		}
		request.getSession().setAttribute("onmanager", tblUser);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登陆本系统。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  管理员登出
	 */
	public String logout(){
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登出本系统。\r\n");
		ServletActionContext.getRequest().getSession().removeAttribute("onmanager");
		return "success";
	}

	/*
	 * 于辉  查询所有的配送员
	 */
	public String findallsender() throws Exception{
		senderList = senderService.findall();
		return "success";
	}
	
	/*
	 * 于辉  隐藏一个配送员
	 */
	public String deletesender() throws Exception{
		peisongren = senderService.findFrontById(id);
		peisongren.setFlag(0);
		senderService.delete(peisongren);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"隐藏了一位配送员："+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  增加一个配送员
	 */
	public String addsender() throws Exception{
		peisongren.setFlag(1);
		senderService.insert(peisongren);
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"增加了一位配送员："+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  根据id查找一个配送员
	 */
	public String viewsenderbyid() throws Exception{
		peisongren = senderService.findFrontById(id);
		return "success";
	}
	
	/*
	 * 于辉  修改一个配送员
	 */
	public String updatesender() throws Exception{
		senderService.update(peisongren);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"修改了一位配送员："+peisongren.getPsrname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  查询所有的客服
	 */
	public String findallcustomerservice() throws Exception{
		csList = cSService.findall();
		return "success";
	}
	
	/*
	 * 于辉  隐藏一个客服
	 */
	public String deletecustomerservice() throws Exception{
		customerservice = cSService.findFrontById(id);
		customerservice.setFlag(0);
		cSService.delete(customerservice);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"隐藏了一位客服："+customerservice.getCsname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  增加一个客服
	 */
	public String addcustomerservice() throws Exception{
		customerservice.setFlag(1);
		customerservice.setPassword(Md5Util.addMd5(password));
		cSService.insert(customerservice);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"增加了一位客服："+customerservice.getCsname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  根据id查找一个客服
	 */
	public String viewcustomerservicebyid() throws Exception{
		customerservice = cSService.findFrontById(id);
		return "success";
	}
	
	/*
	 * 于辉  修改一个客服
	 */
	public String updatecustomerservice() throws Exception{
		cSService.update(customerservice);
		//生成日志
		BuildLogFile.build(Onmanager.manager().getUserName()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"修改了一位客服："+customerservice.getCsname()+"。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  查询所有的用户
	 */
	public String findalluser() throws Exception{
		Map<String, Object> map = userService.findall(pageNo, pageSize, keyword, column);
		userList = (List<?>) map.get("allUser");
		count = (Integer) map.get("allCount");
		
		return "success";
	}
	
	/*
	 * 于辉  以下为各属性的get、set方法
	 */
	public TblUser getTblUser() {
		return tblUser;
	}
	public void setTblUser(TblUser tblUser) {
		this.tblUser = tblUser;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getVerifycode() {
		return verifycode;
	}
	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}
	public List<?> getSenderList() {
		return senderList;
	}
	public void setSenderList(List<?> senderList) {
		this.senderList = senderList;
	}
	public SenderService getSenderService() {
		return senderService;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Peisongren getPeisongren() {
		return peisongren;
	}
	public void setPeisongren(Peisongren peisongren) {
		this.peisongren = peisongren;
	}
	public List<?> getCsList() {
		return csList;
	}
	public void setCsList(List<?> csList) {
		this.csList = csList;
	}
	public Customerservices getCustomerservice() {
		return customerservice;
	}
	public void setCustomerservice(Customerservices customerservice) {
		this.customerservice = customerservice;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<?> getUserList() {
		return userList;
	}
	public void setUserList(List<?> userList) {
		this.userList = userList;
	}

	@Resource(name="managerServiceImpl")
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}
	@Resource(name="senderServiceImpl")
	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}
	@Resource(name="CSServiceImpl")
	public void setcSService(CSService cSService) {
		this.cSService = cSService;
	}
	@Resource(name="userServiceImpl")
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	
	
}
